﻿/*
* Thu Feb 28 12:50:41 2008
* source:bbs.bluedea.com
* 此类有2种使用方法
* 1、target没有定义onRollOver、onRollOut事件的时候 ToolTip.getInstance().setToolTip(btn0, "dasfasdfsd");
* 2、target已经定义onRollOver、onRollOut事件的时候 ToolTip.getInstance().tipMsg = "dfasdfasdf";
*/

import utils.Delegate;
class utils.ToolTip{
	private static var m_instance:ToolTip;
	
	private var m_container;
	private var m_str:String;
	private var m_handle;
	private var m_border:Number = 0x58A6D7;
	private var m_color:Number = 0xD8E4F4;
	private var m_fontColor:Number = 0x004080;
	private var DELTA_NUM:Number = 10;
	private var DEPTH_NUM:Number = 10000;
	
	function ToolTip(){
		m_container = _root;
	}
	
	static function getInstance():ToolTip {
		if(ToolTip.m_instance == undefined){
			ToolTip.m_instance = new ToolTip();
		}
		return ToolTip.m_instance;
    }
	
	/**
	* 增加一个模式，当外部已经定义了onRollOver、onRollOut事件的时候使用
	* tipMsg = ""表示不显示
	*/
	function set tipMsg(str){
		if(str != undefined && str != ""){
			m_str = str;
			showTip();
		}else{
			clearTip();
		}
	}
	
	//target为要设置tip的mc的实例名,str为要显示的tip
	function setToolTip(target:MovieClip, str:String) {
		//把要显示的信息指向到这个按钮的事件上去
		target.onRollOver = Delegate.create(this, showTip, {msg:str});
		target.onRollOut = target.onReleaseOutside = Delegate.create(this, clearTip);
    }
	
	//设置边框，背景，字体颜色
	function setColor(border, color, fontColor) {
		m_border = border;
		m_color = color;
		m_fontColor = fontColor;
	}
	
	private function showTip(obj){
		clearTip();
		m_handle = _global.setTimeout(Delegate.create(this, createTip, {msg:obj.msg}), 1000);
	}
	
	private function clearTip() {
		_global.clearTimeout(m_handle);
		//删除文本框实例
		m_container.tip_mc.removeTextField();
	}
	
	private function createTip(obj) {
		var msg = obj.msg;
		var mcTip = m_container.createTextField("tip_mc", DEPTH_NUM, m_container._xmouse, m_container._ymouse, 0, 0);
		mcTip.autoSize = true;
		mcTip.text = (msg == undefined) ? m_str : msg;
		//设置文本颜色
		mcTip.textColor = m_fontColor;
		//文本不可选　
		mcTip.selectable = false;
		//允许使用边框颜色
		mcTip.border = true;
		//设置边框颜色
		mcTip.borderColor = m_border;
		//允许使用背景色
		mcTip.background = true;
		//设置背景颜色
		mcTip.backgroundColor = m_color;
		setTipLocation();
	}
	
	private function setTipLocation() {
		var tip = m_container.tip_mc;
		tip._x = m_container._xmouse + DELTA_NUM;
		tip._y = m_container._ymouse + DELTA_NUM;
		
		//如果tip超出右边界
		if ((tip._x + tip._width) > Stage.width) {
			//设置tip在鼠标的左边
			tip._x = m_container._xmouse - tip._width - DELTA_NUM;
			//如果tip超出下边界
		}else if((tip._y + tip._height) > Stage.height) {
			//设置tip在鼠标的上边
			tip._y = m_container._ymouse - tip._height - DELTA_NUM;
		}
	}
	
}